package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;

import com.CreateRadomTime;
import com.DBCon;

/**
 * Servlet implementation class ServletCreateCheckForm
 */
@WebServlet({ "/ServletCreateCheckForm", "/Manager/CheckForm" })
public class ServletCreateCheckForm extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String sql;
		String sql_findDepName;
		String sql_count;
		int CheckNum=0;//表示打卡人数
		
		
		ResultSet rs;
		Random rd=new Random();
		DBCon DC=new DBCon();
		
		//获取到了这五天的日期
		String[] test={"是 绿码 否 否 否 是","是 黄码 否 是 否 是","是 红码 是 是 是 是"};	
		
		try {
			//teastastics表的数据查找操作
			sql_count="Select count(IsCheck) as checknum From teastatistics where IsCheck='已完成' ";
			rs=new DBCon().find(sql_count);
			while(rs.next()) {
				//获取打卡记录数
				CheckNum=rs.getInt("checknum");
			}
			String[][] CheckRecord=new String[CheckNum][3]; //总记录数
			
			sql_findDepName="Select TeaId,Date,IsTested From teastatistics where IsCheck='已完成' ";
			
			rs=new DBCon().find(sql_findDepName);
			int i=0;
			while(rs.next()) {
				CheckRecord[i][0]=rs.getString("TeaId"); //教师编号
				CheckRecord[i][1]=rs.getString("Date"); //时间
				CheckRecord[i][2]=rs.getString("IsTested"); //是否核酸
				if(CheckRecord[i][2].equals("已完成")) CheckRecord[i][2]="是";
				else CheckRecord[i][2]="否";
				i++;
			}
			//获取到了全部教师账号
			//写循环			
				for(int j=100;j<200;j++) {
					
					String TeaId=CheckRecord[j][0]; //教师编号
					String Date=CheckRecord[j][1];   //日期
					
					int randrs=rd.nextInt(3);
					String res=test[randrs]; //获取到随机的结果
					String[] res_split=res.split(" "); //将空格划分结果
					String IsChengdu=res_split[0];
					String CodeStatus=res_split[1];
					String IsHealth=res_split[2];
					String IsIsolate=res_split[3];
					String IsContact=res_split[4];
					String IsNuclein=CheckRecord[j][2];
					String IsSure=res_split[5];
					

					sql= "insert into teachercheck values('"+TeaId+"','"+Date+
							"','四川省 成都市 温江区','"+IsChengdu+"','"+CodeStatus+
							"','"+IsHealth+"','"+IsIsolate+"','"+IsContact+"','"+IsNuclein+"','"+IsSure+"')";
					int ret;
					ret=DC.query(sql);
					System.out.println("ret="+ret);
				}
				
		}catch (ClassNotFoundException | SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
